//
//  SwitchViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class SwitchViewController: BaseTableViewController {

    let disposeBag = DisposeBag()
    
    lazy var switchView: UISwitch = {
        let _switchV = UISwitch()
        self.tableHeadView.addSubview(_switchV)
        _switchV.snp.makeConstraints({ (make) in
            make.center.equalTo(self.tableHeadView)
        })
        return _switchV;
    }()
    
    lazy var button: UIButton = {
        let _btn = UIButton(type: .system)
        self.tableHeadView.addSubview(_btn)
        _btn.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.bottom.equalTo(switchView.snp.top).offset(-15)
        })
        _btn.setTitle("按钮状态", for: .normal)
        return _btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupData()
        _rxSwiftProperty()
    }
    
    private func _rxSwiftProperty() {
        switchView.rx.isOn.asObservable().subscribe { print(info: "当前状态: \($0)") }.disposed(by: disposeBag)
        
        // 开关状态与按钮是否可交互绑定
        switchView.rx.isOn.bind(to: button.rx.isEnabled).disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["onTintColor",
                      "tintColor",
                      "onImage",
                      "offImage",
                      "on",
                      "thumbTintColor",
                      "增加事件响应机制"]
        
        let descArr = ["""
                        处于on时switch 的颜色
                        switchView.onTintColor = [UIColor grayColor];
                       """,
                       """
                        处于off时switch 的颜色
                        switchView.tintColor = [UIColor greenColor];
                       """,
                       """
                        设置 on 的图标
                        switchView.onImage = [UIImage imageNamed:@"4.png"];
                       """,
                       """
                        设置 off 的图标
                        switchView.offImage = [UIImage imageNamed:@"4.png"];
                       """,
                       "设置switch的开关",
                       """
                        设置拇指颜色
                        switchView.thumbTintColor = [UIColor redColor];
                       """,
                       """
                        监听的是值改变事件: UIControlEventValueChanged
                        [switchView addTarget:self action:@selector(switchOn) forControlEvents:UIControlEventValueChanged];
                       """]
        
        addTitles(titles, footDescs: descArr)
    }

}
